class Solution:
    """
    方法：使用栈来解码字符串。遇到数字时累积计算，遇到'['时将当前字符串和数字入栈，遇到']'时弹出数字和字符串进行重复拼接。
    
    Args:
        s: str, 需要解码的字符串
        
    Returns:
        str: 解码后的字符串
        
    Time: O(n) - 需要遍历整个字符串
    Space: O(n) - 最坏情况下栈的大小为n/2
    """
    def decodeString(self, s: str) -> str:
        stack = []
        cur_str = ""
        cur_num = 0
        for c in s:
            if c == '[':
                stack.append((cur_str, cur_num))
                cur_num = 0
                cur_str = ""
            elif c == ']':
                pre_str, num = stack.pop()
                cur_str = pre_str + cur_str * num
            elif c.isdigit():
                cur_num = cur_num * 10 + int(c)
            else:
                cur_str += c
        return cur_str